package com.app.oral.controller

import jakarta.servlet.http.HttpSession
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.responses.ApiResponse as SwaggerApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag
import io.swagger.v3.oas.annotations.Hidden

@Controller
@RequestMapping("/admin")
@Tag(name = "管理后台页面", description = "管理后台HTML页面路由")
@Hidden
class AdminController {

    @Operation(summary = "管理后台首页", description = "返回管理后台首页或重定向到登录页")
    @GetMapping
    fun adminHome(@Parameter(hidden = true) session: HttpSession): String {
        // 检查是否已登录
        val isLoggedIn = session.getAttribute("adminLoggedIn") as Boolean?
        return if (isLoggedIn == true) {
            "redirect:/admin/dashboard"
        } else {
            "admin/login"
        }
    }

    @Operation(summary = "登录页面", description = "返回管理员登录页面")
    @GetMapping("/login")
    fun loginPage(): String {
        return "admin/login"
    }

    // 登录API已移至 LoginController (/api/v1/auth/admin/login)

    @Operation(summary = "仪表盘页面", description = "返回管理后台仪表盘页面")
    @GetMapping("/dashboard")
    fun dashboard(
        @Parameter(hidden = true) session: HttpSession,
        @Parameter(hidden = true) model: Model
    ): String {
        // 检查登录状态
        val isLoggedIn = session.getAttribute("adminLoggedIn") as Boolean?
        if (isLoggedIn != true) {
            return "redirect:/admin/login"
        }

        // 添加仪表盘数据（临时模拟数据）
        model.addAttribute("pendingCount", 5)
        model.addAttribute("pendingStudents", 12)
        model.addAttribute("pendingTeachers", 3)
        
        return "admin/dashboard"
    }

    @Operation(summary = "用户管理页面", description = "返回用户管理页面")
    @GetMapping("/users")
    fun users(@Parameter(hidden = true) session: HttpSession): String {
        val isLoggedIn = session.getAttribute("adminLoggedIn") as Boolean?
        if (isLoggedIn != true) {
            return "redirect:/admin/login"
        }
        return "admin/users"
    }

    @Operation(summary = "试卷管理页面", description = "返回试卷管理页面")
    @GetMapping("/exam-papers")
    fun examPapers(@Parameter(hidden = true) session: HttpSession): String {
        val isLoggedIn = session.getAttribute("adminLoggedIn") as Boolean?
        if (isLoggedIn != true) {
            return "redirect:/admin/login"
        }
        return "admin/exam-papers"
    }

    @Operation(summary = "班级管理页面", description = "返回班级管理页面")
    @GetMapping("/classes")
    fun classes(@Parameter(hidden = true) session: HttpSession): String {
        val isLoggedIn = session.getAttribute("adminLoggedIn") as Boolean?
        if (isLoggedIn != true) {
            return "redirect:/admin/login"
        }
        return "admin/classes"
    }
}